﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Simhopp.Model;
using Simhopp.Presenter;

namespace Simhopp.GUI
{
    public partial class frmEditJudges : Form, InterfaceEditJudgesView
    {
        public Competition PropCompetition { get; set; }
        public Judge judge { get; set; }
        public event DelegateAddJudge EventAddJudge = null;
        public event DelegateRemoveJudge EventRemoveJudge = null;
        public event DelegateUpdateJudge EventUpdateJudge = null;
        public event DelegateUpdateJudgeList EventUpdateJudgeList = null;
        public event DelegateUpdateCompleteJudgeList EventUpdateCompleteJudgeList = null;
        public event DelegateAddExistingJudge EventAddExistingJudge = null;
        public event DelegateUpdateCompleteDiverList EventUpdateCompleteDiverList = null;
        public frmEditJudges()
        {
            InitializeComponent();
            cmbNationality.Items.AddRange(Enum.GetNames(typeof(Simhopp.Model.Countries)));

        }

        public frmEditJudges(Competition competition)
        {
            InitializeComponent();
            cmbNationality.Items.AddRange(Enum.GetNames(typeof(Simhopp.Model.Countries)));
            PropCompetition = competition;
        }

        private void frmEditJudges_Load(object sender, EventArgs e)
        {
            PresenterEditJudges pEditJudges = new PresenterEditJudges(this, PropCompetition);
            UpdateJudgeListNew();
            UpdateCompleteJudgeList();
            lblCompetition.Text = PropCompetition.Name.ToString();
        }

        public string GetCountryString()
        {
            string strCountry = cmbNationality.SelectedItem.ToString();
            strCountry = strCountry.Replace("_", " ");
            return strCountry;
        }

        public string JudgeListString()
        {
            string name = txtName.Text.ToString();
            string nationality = GetCountryString();
            string password = txtPassword.Text.ToString();
            string username = txtUsername.Text.ToString();
            string strOut = string.Format("{0, -45} {1, -45} {2, -25} {3}", name, nationality, username, password);

            return strOut;
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (EventAddJudge != null)
            {
                EventAddJudge(new Judge(txtName.Text, 
                    (Countries)Enum.Parse(typeof(Countries), cmbNationality.SelectedItem.ToString()),
                    txtPassword.Text, txtUsername.Text));
               // lstJudges.Items.Add(JudgeListString());
                UpdateJudgeListNew();
            }
        }

        private void UpdateJudgeList()
        {
            lstvwJudges.Items.Clear();
            List<Judge> judgeList ;
            if (EventUpdateJudgeList != null)
            {
                judgeList = EventUpdateJudgeList();
                foreach (Judge j in judgeList)
                {
                    ListViewItem tempListView = new ListViewItem(j.Name);
                    //tempListView.Add(j.Name);
                    tempListView.SubItems.Add(j.Nationality.ToString());
                    tempListView.SubItems.Add(j.Username);
                    tempListView.SubItems.Add(j.Password);
                    lstvwJudges.Items.Add(tempListView);
                }
            }
            foreach (ListViewItem item in lstvwJudges.Items)
            {
                item.BackColor = item.Index % 2 == 0 ?
                    Color.Azure : Color.Snow;
            }
        }

        private void UpdateJudgeListNew()
        {
            lstvwJudges.Items.Clear();
            List<Judge> judgeList;
            if (EventUpdateJudgeList != null)
            {
                judgeList = EventUpdateJudgeList();
                foreach (Judge j in judgeList)
                {
                    ListViewItem tempListView = new ListViewItem();
                    tempListView.Text = j.Name;
                    tempListView.SubItems.Add(j.Nationality.ToString());
                    //tempListView.SubItems.Add(j.Username);
                    //tempListView.SubItems.Add(j.Password);
                    tempListView.Tag = j;
                    lstvwJudges.Items.Add(tempListView);
                }
            }
            foreach (ListViewItem item in lstvwJudges.Items)
            {
                item.BackColor = item.Index % 2 == 0 ?
                    Color.Azure : Color.Snow;
            }
        }

        private void UpdateCompleteJudgeList()
        {
            lstvwJudgesComplete.Items.Clear();
            List<Judge> judgeList = new List<Judge>();
            if (EventUpdateCompleteJudgeList != null)
            {
                EventUpdateCompleteJudgeList(judgeList);
                foreach(Judge j in judgeList)
                {
                    ListViewItem tempListView = new ListViewItem();
                    tempListView.Text = j.Name;
                    tempListView.SubItems.Add(j.Nationality.ToString());
                    tempListView.Tag = j;
                    lstvwJudgesComplete.Items.Add(tempListView);
                }
            }
            foreach (ListViewItem item in lstvwJudgesComplete.Items)
            {
                item.BackColor = item.Index % 2 == 0 ?
                    Color.Azure : Color.Snow;
            }

        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (EventUpdateJudge != null)
            {
                //Måste ha in ID
                EventUpdateJudge(new Judge(judge.ID, txtName.Text,
                    (Countries)Enum.Parse(typeof(Countries), cmbNationality.SelectedItem.ToString()),
                    txtPassword.Text, txtUsername.Text)); //For testing purpose
                //EventUpdateJudge((Judge)lstJudges.SelectedItem);
                //lstJudges.Items[lstJudges.SelectedIndex] = JudgeListString();
                UpdateJudgeListNew();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if(EventRemoveJudge != null)
            {
                EventRemoveJudge(judge);
                //EventRemoveJudge((Judge)lstJudges.SelectedItem);
                UpdateJudgeListNew();
            }
        }

        private void lstvwJudges_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstvwJudges.SelectedItems.Count == 1)
            {
                judge = lstvwJudges.SelectedItems[0].Tag as Judge;
                FillTextBoxes(judge);
            }
        }

        private void FillTextBoxes(Judge judge)
        {
            //txtID.Text = judge.ID.ToString();
            txtName.Text = judge.Name.ToString();
            txtPassword.Text = judge.Password;
            txtUsername.Text = judge.Username;
            cmbNationality.SelectedIndex = (int)judge.Nationality;
        }

        private void btnReturn_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            //Hämta existerande dommare från existerande listan
            //Anropa EventAddExistingJudge(domare)
            Judge judge = lstvwJudgesComplete.SelectedItems[0].Tag as Judge;
            EventAddExistingJudge(judge);
            UpdateJudgeListNew();
        }

        
    }
}
